﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PracticeAzure.Website.Shared
{
    public class RenderEngine
    {
        private IEnumerable<IRender> _renders;

        private static RenderEngine _engine;
        public static RenderEngine Current
        {
            get
            {
                return _engine;
            }
        }

        static RenderEngine()
        {
            _engine = new RenderEngine();
            _engine._renders = new List<IRender>()
            {
                // the new line can be done by the jquery html editor
                //new NewLineRender(),
                new CodeBlockRender()
            };
        }

        public string Render(string content)
        {
            var result = content;
            foreach (var render in _renders)
            {
                result = render.Render(result);
            }
            return result;
        }

        private class NewLineRender : IRender
        {
            public string Render(string content)
            {
                return content
                    .Replace("\r\n", "<br />");
            }
        }

        private class CodeBlockRender : IRender
        {
            public string Render(string content)
            {
                return content
                    .Replace("[code]", "<pre>")
                    .Replace("[/code]", "</pre>");
            }
        }
    }

    public interface IRender
    {
        string Render(string content);
    }
}